﻿/***********************************************************************
 *            Project: CoreCms.Net                                     *
 *                Web: https://CoreCms.Net                             *
 *        ProjectName: 核心内容管理系统                                *
 *             Author: 大灰灰                                          *
 *              Email: JianWeie@163.com                                *
 *         CreateTime: 2020-02-26 0:57:51
 *        Description: 暂无
 ***********************************************************************/


using CoreCms.Net.Configuration;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CoreCms.Net.Utility.Helper
{
    /// <summary>
    /// 短信相关帮助类
    /// </summary>
    public class SmsHelper
    {
        public static string Submail_Appid = "63329";
        public static string Submail_Appkey = "b469cdce1a9df6f20a1f76a871a3c6cc";

        /// <summary>
        /// 根据消息分类和传输参数,获取要发送的内容
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="parameters">参数类型</param>
        /// <returns></returns>
        public static string GetTemp(string type, JObject parameters, string language = "zh-CN")
        {
            var msg = string.Empty;
            if (type == GlobalEnumVars.SmsMessageTypes.Reg.ToString())
            {
                // 账户注册
                var code = string.Empty;
                if (parameters.ContainsKey("code"))
                {
                    code = parameters["code"]?.ToString();
                }
                msg = "您正在注册账号，验证码是" + code + "，请勿告诉他人。";
            }
            else if (type == GlobalEnumVars.SmsMessageTypes.Login.ToString())
            {
                // 账户登录
                var code = string.Empty;
                if (parameters.ContainsKey("code"))
                {
                    code = parameters["code"]?.ToString();
                }
                msg = "您正在登陆账号，验证码是" + code + "，请勿告诉他人。";
            }
            else if (type == GlobalEnumVars.SmsMessageTypes.Veri.ToString())
            {
                // 验证验证码
                var code = string.Empty;
                if (parameters.ContainsKey("code"))
                {
                    code = parameters["code"]?.ToString();
                }
                msg = "您的验证码是" + code + "，请勿告诉他人。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.CreateOrder.ToString())
            {
                // 订单创建
                msg = "恭喜您，订单创建成功，祝您购物愉快。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.OrderPayed.ToString())
            {
                // 订单支付通知买家
                msg = "恭喜您，订单支付成功，祝您购物愉快。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.RemindOrderPay.ToString())
            {
                // 未支付催单
                msg = "您的订单还有1个小时就要取消了，请及时进行支付。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.DeliveryNotice.ToString())
            {
                // 订单发货
                msg = "您好，您的订单已经发货。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.AfterSalesPass.ToString())
            {
                // 售后审核通过
                msg = "您好，您的售后已经通过。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.RefundSuccess.ToString())
            {
                // 退款已处理
                msg = "用户您好，您的退款已经处理，请确认。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.SellerOrderNotice.ToString())
            {
                // 订单支付通知卖家
                msg = "您有新的订单了，请及时处理。";
            }
            else if (type == GlobalEnumVars.PlatformMessageTypes.Common.ToString())
            {
                //通用类型
                var tpl = string.Empty;
                if (parameters.ContainsKey("tpl"))
                {
                    tpl = parameters["tpl"]?.ToString();
                }
                msg = tpl;
            }
            return msg;
        }

        /// <summary>
        /// //记录哪里需要发送消息，统一处理
        /// </summary>
        public static void SendMessage()
        {
            //记录哪里需要发送消息，统一处理
        }

        //发送国际短信
        public static async Task<string> SendSmsAsync(string to, string content, string sign_type = "normal", string sign_version = "2")
        {
            string result2 = await HttpHelper.HttpGetAsync("https://api-v4.mysubmail.com/service/timestamp.json");
            //string to = "+852176xxx5149";
            //string content = "【xxx】你好，你的验证码是2257";
            //string sign_type = "md5";             //加密方式有normal，md5，sha1
            //string sign_version = "2";
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("appid", Submail_Appid);
            d.Add("to", to);
            //如果不使用加密方式或者signature传normal则signature参数传入appkey的值
            if (sign_type.Equals("md5") || sign_type.Equals("sha1"))
            {
                string result = await HttpHelper.HttpGetAsync("https://api-v4.mysubmail.com/service/timestamp.json");
                string timestamp = JObject.Parse(result)["timestamp"].ToString();
                d.Add("sign_type", sign_type);
                d.Add("timestamp", timestamp);
                d.Add("sign_version", sign_version);
                if (sign_type.Equals("md5"))
                {
                    d.Add("signature", CommonHelper.GetMD5Signature(CommonHelper.FormatRequest(d), Submail_Appid, Submail_Appkey));
                }
                else
                {
                    d.Add("signature", CommonHelper.GetSHA1Signature(CommonHelper.FormatRequest(d), Submail_Appid, Submail_Appkey));
                }
            }
            else
            {
                d.Add("signature", Submail_Appkey);
            }
            d.Add("content", content);
            string response = await HttpHelper.HttpPostAsync("https://api-v4.mysubmail.com/internationalsms/send.json", d);
            return response;
        }
    }
}
